﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace FCTIP20.Procdure
{
    internal class ConfigHelper
    {
        public static Config Load(string fileName)
        {
            if (!File.Exists(fileName))
            {
                switch (fileName)
                {
                    case "config.xml":
                        return CreateListSample(fileName);

                    default: break;
                }
            }
            XmlSerializer serializer = new XmlSerializer(typeof(Config));

            TextReader reader = new StreamReader(fileName);
            // 初始化默认值

            var obj = serializer.Deserialize(reader);
            reader.Close();
            return obj as Config;
        }

        public static void SaveConfig(Config config, string fileName)
        {
            var dir = "config_backup";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var temp = fileName.Length - 4;
            string tmp = $"./{dir}/config-{fileName.Substring(0, temp)}{DateTime.Now.ToString("yyyyMMdd")}.xml";
            if (File.Exists(tmp))
            {
                File.Delete(tmp);
            }

            XmlSerializer serializer = new XmlSerializer(typeof(Config));
            TextWriter writer = new StreamWriter(tmp);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            File.Copy(tmp, fileName);
        }

        private static Config CreateListSample(string fileName)
        {
            Config config = new Config()
            {
                OkCount = 0,
                NgCount = 0,
                IOModule1 = "COM11",
                ProductCom = 12,
                PringterIP = "127.0.0.88",
                ReadTimes = 0,
                ErrorTimes = 0,
                ListConfigs = new List<ListConfigs>
                {
                     new ListConfigs
                     {
                           MoBanName="JQ8930P",
                           EANValue = "4099987083256",
                     },
                     new ListConfigs
                     {
                           MoBanName="JQ8930E",
                           EANValue = "4099987083263",
                     },
                     new ListConfigs
                     {
                           MoBanName="模块",
                           EANValue = "",
                     }
                },
                ModelListConfigs = new List<ModelListConfigs>
                {
                    new ModelListConfigs
                     {
                           ModelName="89301",
                           EANValue = "4099987083270",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89302",
                           EANValue = "4099987083287",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89303",
                           EANValue = "4099987083294",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89304",
                           EANValue = "4099987083300",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89305",
                           EANValue = "4099987083317",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89306",
                           EANValue = "4099987083324",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89307",
                           EANValue = "4099987083331",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89308",
                           EANValue = "4099987083348",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89309",
                           EANValue = "4099987083355",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89310",
                           EANValue = "4099987083362",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89311",
                           EANValue = "4099987083379",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89312",
                           EANValue = "4099987083386",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89313",
                           EANValue = "4099987083393",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89314",
                           EANValue = "4099987083409",
                     },
                     new ModelListConfigs
                     {
                           ModelName="89315",
                           EANValue = "4099987083416",
                     }
                }
            };
            SaveConfig(config, fileName);
            return config;
        }

    }
}
